package panda.query.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import panda.query.condition.AttributeName;
import panda.query.condition.ValueExpression;
import panda.query.scan.ResultSetScanner;
import panda.query.scan.Scanner;
import panda.query.struct.Attribute;
import panda.query.struct.SemAttribute;
import panda.record.Schema;
import panda.transaction.Transaction;

public class ResultSetNode implements QueryTreeNode {
	
	QueryTreeNode tn;
	Transaction tx;
	Map<Attribute, ValueExpression> map;
	Schema sch = new Schema();
	
	public ResultSetNode(QueryTreeNode tn, ArrayList<ValueExpression> ve, boolean star, Transaction tx) {
		this.tn = tn;
		this.tx = tx;
//		this.ve = ve;
		
		map = new HashMap<Attribute, ValueExpression>();
		
		//TODO Support other types
		
		for (int i = 0; i < ve.size(); i++) {
			SemAttribute sa;
			
			if (star)
				sa = new SemAttribute(((AttributeName)ve.get(i)).getAttribute().getAttributeName());
			else
				sa = new SemAttribute("column" + i);
			
			map.put(sa, ve.get(i));
			sch.addIntAttr(sa);
		}
	}
	
	@Override
	public Scanner open() {
		return new ResultSetScanner(tn.open(), map, tx);
	}

	@Override
	public int distinctSize(Attribute attr) {
		return tn.distinctSize(attr);
	}

	@Override
	public Schema getSchema() {
		return sch;
	}

	@Override
	public int outputSize() {
		return tn.outputSize();
	}

	@Override
	public int blockAccessed() {
		return tn.blockAccessed();
	}
}
